package c926.xiaoduyun.xiaodu.plugin.other;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import org.apache.commons.lang.StringUtils;

import java.util.EventObject;
import java.util.List;
import java.util.Map;

/**
 * 类描述：一键删除附件面板上的所有附件功能插件
 */
public class DeleteAllAttPanelFormPlugin extends AbstractFormPlugin {
    //删除按钮标识
    private static final String DEL_ALL_ATT_BT_NAME = "c926_deleteallatt";
    //附件面板标识
    private static final String ATTACHMENT_PANEL_NAME = "attachmentpanel";

    @Override
    public void registerListener(EventObject e) {
        addItemClickListeners("tbmain");

    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        String itemKey = evt.getItemKey();
        if(!StringUtils.equals(itemKey,DEL_ALL_ATT_BT_NAME)){
            return;
        }
        deleteAllAttachmentPanelData();
    }

    /**
     * 删除附件面板上的所有附件方法
     */
    private void deleteAllAttachmentPanelData() {
        AttachmentPanel panel = getControl(ATTACHMENT_PANEL_NAME);
        List<Map<String, Object>> attachmentData = panel.getAttachmentData();
        if(CollectionUtils.isEmpty(attachmentData)){
            return;
        }
        //删除附件面板上的所有附件，并通知前端移除页面上的附件
	    //remove()方法底层会调用相关方法删除数据库附件信息与附件
        attachmentData.forEach(panel::remove);
    }
}

